# -*- coding: utf-8 -*-


from vedar1 import sentiment_score
import csv
from selenium import webdriver
import urlparse



max_page = 10
c_list =[]

driver = webdriver.Chrome("C:\Python27\chromedriver_win32\chromedriver.exe")
driver.get('https://www.tripadvisor.in/Attraction_Review-g304558-d1221401-Reviews-Kolkata_Zoo_Zoological_Garden-Kolkata_Calcutta_Kolkata_District_West_Bengal.html')

#Extracting comments

comments = driver.find_elements_by_xpath('//*[@class="location-review-review-list-parts-ExpandableReview__reviewText--gOmRC"]')
for i in range(len(comments)):
    #print (comments[i].text + " ; ").encode('utf-8')
    c_list.append(comments[i].text.encode('utf-8'))


for a in range(100):
    next_page_url = driver.find_elements_by_xpath('//*[@class="ui_button nav next primary "]')[0].get_attribute("href")
    url1= "http://tripadvisor.in"
    absolute_next_page_url = urlparse.urljoin(url1,next_page_url)
    print absolute_next_page_url
    driver.get(absolute_next_page_url)
    comments = driver.find_elements_by_xpath('//*[@class="location-review-review-list-parts-ExpandableReview__reviewText--gOmRC"]')
    for i in range(len(comments)):
       #print (comments[i].text+";").encode('utf-8')
       c_list.append(comments[i].text.encode('utf-8'))



driver.close()
############################################################################################################################
print c_list
pos =0.00
neg =0.00
neu=0.00
l= len(c_list)
with open('result2.csv', 'wb') as csvfile:

    fieldnames =['Comments','Sentiment','Positive%','Negative%','Neutral%']

    thewriter = csv.DictWriter(csvfile, fieldnames=fieldnames)

    thewriter.writeheader()
    

    for comment in c_list:
        senti = sentiment_score(comment)
        thewriter.writerow({'Comments':comment,'Sentiment':senti})
        if senti == 1:
            pos += 1
        if senti == 0:
            neu += 1
        if senti == -1:
            neg += 1
    
    thewriter.writerow({'Positive%':(pos/l)*100,'Negative%':(neg/l)*100,'Neutral%':(neu/l)*100})  

###########################################################################################################################################################################
##Copyright (C) 2020 ar fcr gr

##This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

##This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

##You should have received a copy of the GNU General Public License along with this program. If not, see https://www.gnu.org/licenses/.
############################################################################################################################################################################   
